/**
 * Copyright (c) 1987-2010 Fujian Fujitsu Communication Software Co.,
 * Ltd. All Rights Reserved.
 * This software is the confidential and proprietary information of
 * Fujian Fujitsu Communication Software Co., Ltd.
 * ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with FFCS.
 * FFCS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. FFCS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package aip.core.dao.dictionary;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.dao.DataAccessException;
import org.springframework.util.Assert;

import aip.core.model.dictionary.Dictionary;
import aip.ui.dictionary.cache.DictionaryCache;

import com.googlecode.horncomb.bean.filter.SimpleMapConditionSet;
import com.googlecode.horncomb.bean.order.DefaultOrder;
import com.googlecode.horncomb.bean.order.DefaultOrderToken;
import com.googlecode.horncomb.orm.ibatis.SqlMapEntityDaoImpl;

/**
 * Dictionary data access object.
 *
 * @author
 * @version Revision 1.0.0
 */
public class DictionaryDao extends SqlMapEntityDaoImpl<Dictionary> {

    private static final long serialVersionUID = 1661107534795704973L;

    private DictionaryCache   dictionaryCache;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initDao() throws Exception {
        super.initDao();
        if (this.dictionaryCache != null) {
            Collection<? extends Dictionary> dicts = this.queryAll();
            for (Dictionary dict : dicts) {
                this.dictionaryCache.putInCache(dict);
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void save(Dictionary entity) throws DataAccessException {
        super.save(entity);
        this.dictionaryCache.putInCache(entity);
    }

    /**
     * {@inheritDoc}
     */
    public String getStrDictionary(String key) throws DataAccessException {
        Assert.hasText(key);
        if (dictionaryCache != null) {
            return dictionaryCache.getFromCache(key);
        }
        Dictionary dictionary = this.getById(key);
        if (dictionary != null) {
            return dictionary.getValue();
        }
        return null;
    }

    /**
     * 根据showInCommon取字典信息.
     *
     * @param showInCommon
     *            是否普通配置
     * @throws DataAccessException
     */
    @SuppressWarnings("unchecked")
    public List<Dictionary> findByShowInCommon(Boolean showInCommon)
        throws DataAccessException {
        //Assert.isNull(showInCommon);
        return (List<Dictionary>) this.query(new SimpleMapConditionSet()
            .addConstraint("showInCommon", showInCommon), new DefaultOrder(
            new DefaultOrderToken("ORDER_", true)));
    }

    /**
     * 根据ids取字典信息.
     *
     * @param ids
     * @throws DataAccessException
     */
    @SuppressWarnings("unchecked")
    public List<Dictionary> findByIds(Set<String> ids)
        throws DataAccessException {
        Assert.notEmpty(ids);
        return (List<Dictionary>) this.query(new SimpleMapConditionSet()
            .addConstraint("ids", ids.toArray(new String[] {})),
            new DefaultOrder(new DefaultOrderToken("ORDER_", true)));
    }

    /**
     * @param dictionaryCache
     *            设置dictionaryCache属性
     */
    public void setDictionaryCache(DictionaryCache dictionaryCache) {
        this.dictionaryCache = dictionaryCache;
    }
}
